package com.hx.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;

/*
 * 使用POI库对word进行处理的工具类
 */
public class WordPOIUtils {
    public static void mergeColumeAndRow(String sourceFile, String targetFile) {
        try {
//          String fileName = "D:/案件报告.docx"; 
//          String fileName = "E:/nctcFile/模板/测试合并列.docx";
            XWPFDocument doc = new XWPFDocument(new FileInputStream(sourceFile));

            List<XWPFTable> XWPFtables = doc.getTables();
            for (XWPFTable table : XWPFtables) {
                //合并列：如果表格的第一列的上下两个单元格相同，进行合并。
                for(int row =0 ;row < table.getRows().size(); row++) {//每一行
                    if (row == table.getRows().size() -1) continue; //最后一行跨过去

                    XWPFTableCell cell0 = table.getRow(row).getCell(0);
                    XWPFTableCell cell1 = table.getRow(row + 1).getCell(0);
                    if(cell0.getText().equals(cell1.getText())) {
//                      System.out.println("这两个列一样");
                        // First Row, 设置合并的开始点
                        CTVMerge vmerge = CTVMerge.Factory.newInstance();
                        vmerge.setVal(STMerge.RESTART);
                        cell0.getCTTc().getTcPr().setVMerge(vmerge);
                        cell1.getCTTc().getTcPr().setVMerge(vmerge);

                         // Second Row cell will be merged ,设置合并的结束点
                        CTVMerge vmerge1 = CTVMerge.Factory.newInstance();
                        vmerge1.setVal(STMerge.CONTINUE);
                        cell0.getCTTc().getTcPr().setVMerge(vmerge1);
                        cell1.getCTTc().getTcPr().setVMerge(vmerge1);
                    }
                }
                //合并行：如果一行中最后面的单元格全是空行，则合并它们
                for(XWPFTableRow currentRow : table.getRows()) {
                    int cellHasText = 0;//记录从第几个单元格以后开始为空的
                    for(int i = currentRow.getTableCells().size() -1; i>= 0 ;i--) {
                        if( !currentRow.getCell(i).getText().equals("")) {
                            cellHasText = i;
                            break;
                        }
                    }
                    if(cellHasText > 0) {//开始合并
                        for(int i = cellHasText; i < currentRow.getTableCells().size() ;i++) {
//                          System.out.println("开始合并行了");
                            if (i == cellHasText){
                                currentRow.getCell(i).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);    
                            } else {
                                currentRow.getCell(i).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                            }
                        }
                    }
                }
            }

            FileOutputStream fos = new FileOutputStream(targetFile);  
            doc.write(fos);  
            fos.close();  
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception {  
        String sourceFile = "E:/nctcFile/报告/temp.docx";
        String targetFile = "E:/nctcFile/报告/temp-result.docx";
        mergeColumeAndRow(sourceFile, targetFile);
    }  
}
